import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
} from '@nestjs/common';
import { CertificateService } from './certificate.service';
import { CreateCertificateDto } from './dto/create-certificate.dto';
import { UpdateCertificateDto } from './dto/update-certificate.dto';
import { Certificate } from './entities/certificate.entity';
import { Response } from '../../infra/schema/response';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiParam,
  ApiBody,
} from '@nestjs/swagger';
import { Public } from 'src/auth/auth.decorator';

/**
 * 医师资格证书相关接口控制器
 */
@Public()
@ApiTags('医师资格证书')
@Controller('certificates')
export class CertificateController {
  constructor(private readonly certificateService: CertificateService) {}

  /**
   * 创建医师资格证书信息
   * @param createCertificateDto - 证书创建信息
   * @returns 创建成功的证书信息
   */
  @Post()
  @ApiOperation({ summary: '创建医师资格证书信息' })
  @ApiBody({ type: CreateCertificateDto })
  @ApiResponse({ status: 201, description: '证书创建成功', type: Certificate })
  async create(@Body() createCertificateDto: CreateCertificateDto) {
    await this.certificateService.create(createCertificateDto);
    return Response.success();
  }

  /**
   * 获取所有医师资格证书信息
   * @returns 所有证书信息列表
   */
  @Public()
  @Get()
  @ApiOperation({ summary: '获取所有医师资格证书信息' })
  @ApiResponse({
    status: 200,
    description: '成功获取证书列表',
    type: [Certificate],
  })
  async findAll(
    @Query('page') page: number = 1,
    @Query('size') size: number = 10,
    @Query('title') title: string = '',
  ) {
    const query = { page, size, title };
    const result = await this.certificateService.getList(query);
    return Response.successList(result.data, result.total);
  }

  /**
   * 根据 ID 获取单个医师资格证书信息
   * @param id - 证书 ID
   * @returns 对应 ID 的证书信息
   */
  @Get(':id')
  @ApiOperation({ summary: '根据 ID 获取单个医师资格证书信息' })
  @ApiParam({ name: 'id', description: '证书 ID', type: Number })
  @ApiResponse({
    status: 200,
    description: '成功获取证书信息',
    type: Certificate,
  })
  @ApiResponse({ status: 404, description: '未找到对应证书' })
  async findOne(@Param('id') id: string) {
    const result = await this.certificateService.findOne(+id);
    return Response.success(result);
  }

  /**
   * 根据 ID 更新医师资格证书信息
   * @param id - 证书 ID
   * @param updateCertificateDto - 证书更新信息
   * @returns 更新后的证书信息
   */
  @Put(':id')
  @ApiOperation({ summary: '根据 ID 更新医师资格证书信息' })
  @ApiParam({ name: 'id', description: '证书 ID', type: Number })
  @ApiBody({ type: UpdateCertificateDto })
  @ApiResponse({
    status: 200,
    description: '证书信息更新成功',
    type: Certificate,
  })
  @ApiResponse({ status: 404, description: '未找到对应证书' })
  async update(
    @Param('id') id: string,
    @Body() updateCertificateDto: UpdateCertificateDto,
  ) {
    const result = await this.certificateService.update(
      +id,
      updateCertificateDto,
    );
    return Response.success(result);
  }

  /**
   * 根据 ID 删除医师资格证书信息
   * @param id - 证书 ID
   * @returns 删除操作结果
   */
  @Delete(':id')
  @ApiOperation({ summary: '根据 ID 删除医师资格证书信息' })
  @ApiParam({ name: 'id', description: '证书 ID', type: Number })
  @ApiResponse({ status: 200, description: '证书删除成功' })
  @ApiResponse({ status: 404, description: '未找到对应证书' })
  async remove(@Param('id') id: string) {
    const result = await this.certificateService.remove(+id);
    return Response.success(result);
  }
}
